let path = require('path');
let webpack = require('webpack');
let HTMLWebpackPlugin = require('html-webpack-plugin');
let CleanWebpackPlugin = require('clean-webpack-plugin');
let ExtractTextPlugin = require("extract-text-webpack-plugin");//抽离css样式,防止将样式打包在js中
let UglifyjsPlugin = require('uglifyjs-webpack-plugin');
let webpackMerge = require('webpack-merge');
let CopyWebpackPlugin = require('copy-webpack-plugin');//拷贝资源
let dev = require('./webpack.dev');
let prod = require('./webpack.prod');

const NODE_ENV = process.env.NODE_ENV;
//生产环境构建，生成dist代码压缩
const ENV_PROD = NODE_ENV == 'production';
//开发环境构建，生成dist代码压缩
// const ENV_DEV = NODE_ENV == 'development';
//开发环境构建，启动服务，不压缩代码
const ENV_DEV_SERVER = NODE_ENV == 'dev';

let plugins = [
    new ExtractTextPlugin("style/[name].[chunkhash].css"),
    new webpack.optimize.CommonsChunkPlugin({//提取公共模块
        name:'common'
      }),
    new CleanWebpackPlugin(['./dist']),
    new CopyWebpackPlugin([
    {
        from: __dirname + '/plugins/**',
        to:__dirname +'/dist'
    },
    // {
    //   from: __dirname + '/files/**',
    //   to:__dirname +'/dist'
    // },
    {
      from : __dirname + '/images/**',
      to : __dirname + '/dist'
    },
    {
      from : __dirname + '/404.html',
      to : __dirname + '/dist'
    }
    // {
    //   from : __dirname + '/query.html',
    //   to : __dirname + '/dist'
    // },
    // {
    //   from : __dirname + '/active/**',
    //   to : __dirname + '/dist'
    // }
    ]),
    new HTMLWebpackPlugin({
        //以目录形式访问页面，不用加.html后缀
        filename:'index.html',
        template:'./index.html',
        favicon:'./favicon.ico',
        minify:{
          //移除空白
          collapseWhitespace:true,
          //压缩css
          minifyCSS:true,
          //压缩js
          minifyJS:true
        }
      }),
]
// if(ENV_DEV_SERVER){
//     plugins.push(new webpack.HotModuleReplacementPlugin())
//     plugins.push(new webpack.NamedModulesPlugin())
// }
if(!ENV_DEV_SERVER){
  plugins.push(
    new UglifyjsPlugin({
      uglifyOptions:{
        ie8:true
      }
    })
  )
}

module.exports = webpackMerge(ENV_DEV_SERVER ? dev : prod , {
  entry: ['es5-shim', './src/index'],
  plugins:plugins,
  resolve:{
    extensions:['.js', '.json', '.less', '.css']
  },
  module:{
    rules:[
        {
          test:/\.js$/,
          exclude:/node_modules|libs/,
          use:[{
            loader:'babel-loader',
            options: {
              presets:['es2015'],
              plugins:[
                'transform-remove-strict-mode'
              ]
            }
          }]
        },
        {
          test:/\.js$/,
          use:[{
            loader:'nui-loader',
            options:{
              paths:{
                pub:'src/public',
                layout:'src/platform/p.layout'
              },
              alias:{
                nuijs:'nuonuo-libs/script/nui/index',
                util:'{pub}/util',
                store:'{pub}/store',
                data:'{pub}/data',
                frame:'{layout}/frame',
                layer:'{pub}/logic/layer',
                router:'{pub}/component/platformRouter',
                JSON:'nuonuo-libs/script/polyfill/json'
              }
            }
          }]
        },
        {
          test:/\.(css|less)$/,
          use:ExtractTextPlugin.extract({
            use:[{
              loader:'css-loader',
              options:{
                minimize:!ENV_DEV_SERVER
              }
            }, 'less-loader']
          }),
        },
        {
          test:/\.(svg|ttf|woff|eot)(\?.*)?$/,
          use: [
            {
              loader:'file-loader',
              options:{
                name:'[name].[hash:7].[ext]',
                publicPath:'/font/',
                outputPath:'font/'
              }
            }
          ]
        },
        {
          test:/\.(png|jpe?g|gif)(\?.*)?$/,
          use: [
            {
              loader:'file-loader',
              options:{
                name:'[name].[hash:7].[ext]',
                publicPath:'/images/',
                outputPath:'images/'
              }
            }
          ]
        }
      ]
  },
  output:{
    filename:'script/[name].[hash].js',
    path:path.resolve(__dirname, './dist')
  }
})